package com.sockettest.socketv1;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;

public class ServeSockeTest {
    public static Set<Socket> allSocket;//大厅用户
    public static void main(String[] args) {
            ServerSocket server = null ;
            try {
                //创建ServerSocket对象，指定端口是
                server = new ServerSocket(8080);
                System.out.println("服务启动成功");
            } catch (Exception e) {
                System.out.println("服务器启动出错");
            }
            Socket socket = null;
            try {
                allSocket = new HashSet<>();
                //调用ServerSocket的accept方法，可以接受客户端的请求
                ServerThread st;
                while(true){
                    socket = server.accept();//客户通道
                    allSocket.add(socket);
                    System.out.println("socket上线成功："+socket);
                    //启动服务线程
                    st = new ServerThread(socket);
                    st.start();//服务端接客户端数据
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}
